载入Lua脚本
一、通过文件
    using UnityEngine;
    using System.Collections;
    using XLua;
    public class ByFile : MonoBehaviour {
        LuaEnv luaenv = null;
        void Start()
        {
            luaenv = new LuaEnv();
            // 使用默认loader加载Resources下的"byfile.lua.txt"文件
            luaenv.DoString("require 'byfile'");
        }
        void Update()
        {
            if (luaenv != null)
            {
                luaenv.Tick();
            }
        }
        void OnDestroy()
        {
            luaenv.Dispose();
        }
    }
将byfile.lua.txt存放在Resources文件夹下
    print('hello world')
二、通过字符串
    /*
     *  created by shenjun
     */
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    /*
     * 1、引入命名空间
     */
    using XLua;
    namespace shenjun
    {
        public class ByString : MonoBehaviour {
            LuaEnv luaEnv = new LuaEnv();
            [Multiline(10)]
            public string luaStr1 = 
                @"function Show(...)
                     local t = {...}
                     for i=1,#t do
                         print(t[i])
                     end
                  end
                  Show(5,3,4,1,2)";
            [Multiline(10)]
            public string luaStr2 = 
                @"function GetResult()
                      return 1, '2'
                  end
                  return GetResult()";
            void Start () {
                // public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env = null)
                // 参数1: Lua代码
                // 参数2: 发生error时debug显示信息时使用,指明某某代码块的某行错误
                // 参数3: 为这个代码块
                // 返回值:Lua代码的返回值
                // 执行Lua代码
                luaEnv.DoString("print('Hello World!')");
                luaEnv.DoString(luaStr1);
                object[] results = luaEnv.DoString(luaStr2);
                if(results != null)
                {
                    foreach (var item in results)
                    {
                        Debug.Log(item);
                    }
                }
            }
            void Update () {
                if(luaEnv != null)
                {
                    // 清除Lua未手动调用的LuaBase(比如LuaTable,LuaFunction),及其他一些事情。
                    // 需要定期调用。
                    luaEnv.Tick();
                }
            }
            private void OnDestroy()
            {
                luaEnv.Dispose();
            }
        }
    }
三、通过加载器
Example01 :
    using UnityEngine;
    using System.Collections;
    using XLua;
    public class CustomLoader : MonoBehaviour {
        LuaEnv luaenv = null;
        void Start()
        {
            luaenv = new LuaEnv();
            luaenv.AddLoader((ref string filename) => {
                 if (filename == "InMemory")
                 {
                     string script = "return {ccc = 9999}";
                     return System.Text.Encoding.UTF8.GetBytes(script);
                 }
                 return null;
             });
            luaenv.DoString("print('InMemory.ccc=', require('InMemory').ccc)");
        }
        void Update()
        {
            if (luaenv != null)
            {
                luaenv.Tick();
            }
        }
        void OnDestroy()
        {
            luaenv.Dispose();
        }
    }
Example02 :
    /*
     *  created by shenjun
     */
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using XLua;
    namespace shenjun
    {
        public class ByLoader : MonoBehaviour {
            LuaEnv luaEnv = new LuaEnv();
            void Start () {
                // 注册自定义loader
                luaEnv.AddLoader((ref string filePath)=>{
                    string path = Application.dataPath + "/LessonXLua_shenjun/01_LoadLuaScript/03_ByLoader/" + filePath + ".lua.txt";
                    if(File.Exists(path))
                    {
                        string text = "";
                        using (StreamReader sr = new StreamReader(path))
                        {
                            try
                            {
                                text = sr.ReadToEnd();
                            }
                            catch(System.Exception ex)
                            {
                                Debug.LogError("ReadFile Error :" + path + "; ErrorMsg :" + ex.Message);
                                return null;
                            }
                        }
                        return System.Text.Encoding.UTF8.GetBytes(text);
                        //byte[] buffer;
                        //using(FileStream fsReader = new FileStream(path, FileMode.Open, FileAccess.Read))
                        //{
                        //    buffer = new byte[fsReader.Length];
                        //    try
                        //    {
                        //        fsReader.Read(buffer, 0, (int)fsReader.Length);
                        //        return buffer;
                        //    }
                        //    catch
                        //    {
                        //        return null;
                        //    }
                        //}
                    }
                    else
                    {
                        return null;
                    }
                });
                // 如果注册了自定义的loader,会先查找自定义loader,如果自定义loader返回null,则会使用默认loader,如果默认loader也没找到,则报错。
                luaEnv.DoString("require 'byloader'");
            }
            void Update () {
                if(luaEnv != null)
                {
                    luaEnv.Tick();
                }
            }
            private void OnDestroy()
            {
                luaEnv.Dispose();
            }
        }
    }
byloader.lua.txt
    print('byCustomLoader: hello world')
🔚